﻿package CutLoad.components.window
{
	import CutLoad.*;
	import CutLoad.components.*;
	import CutLoad.components.container.*;
	import CutLoad.components.window.*;
	import CutLoad.cookies.*;
	
	import flash.events.MouseEvent;
	import flash.text.TextFieldAutoSize;
	
	/**
	 * 弹出框的界面，注：用它不只可以做弹出框 
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 
	 */	
	public class CAlert extends Base
	{
		//当前装内容的容器
		private var contendContainer:Container;
		//内容
		private var txtContent:CTextField;
		//
		private var window:CWindow
		//按钮组
		private var arrBtns:Array;
		private var btnFun:Function=null;
		//按钮容器
		private var BtnContainer:ContainerH;
		//
		private var funClerBar:Function=null
		public function CAlert()
		{
		}
		/**
		 * 
		 * @param strTitle 窗口标题
		 * @param strContent 内容
		 * @param arrBtns 按钮标签
		 * @param click 各按钮事件处理的统一处理 ，函数格式：click(按钮索引:Number)
		 * @param isDrag 是否要拖动，默认true可以
		 * @param w 宽
		 * @param h 高
		 * 
		 */
		public function init(strTitle:String,strContent:String,arrBtns:Array,click:Function,isDrag:Boolean=true,w:Number=200,h:Number=100):void{
			btnFun=click;
			//
			window=new CWindow();
			window.init(strTitle,false,isDrag,false,w,h,this);
			window.show(this);
			window.setWH(w,h);
			//
			this.arrBtns=arrBtns;
			setWH(w,h);
			
			contendContainer=new Container();
			contendContainer.init({w:window.getContainer().getShowWidth(),h:window.getContainer().getShowHeight(),isAuto:false});
			//
			txtContent=new CTextField();
			txtContent.init(strContent,CTextField.getTextFormat(1,0x000000,14),false);
			//txtContent.getTextField().border=true;
			txtContent.getTextField().wordWrap=true;
			txtContent.getTextField().autoSize=TextFieldAutoSize.LEFT;
			txtContent.getTextField().wordWrap=true;
			resizeTxtContent()
			//
			contendContainer.append(txtContent,false,{x:0,y:0});
			//
			BtnContainer=new ContainerH();
			BtnContainer.init({w:window.getContainer().getShowWidth(),h:window.getContainer().getShowHeight()-txtContent.getShowHeight(),xspace:5,yspace:5});
			//
			//添加按钮组
		    var isBtnContainerUpdate:Boolean=false;
			for(var i:Number=0;i<arrBtns.length;i++){
				var temBtn:CButton=new CButton();
				temBtn.init(arrBtns[i]);
				temBtn.setOtherInfo({CAlertBtnsId:i});
				temBtn.addEventListener(MouseEvent.CLICK,btnsClick);
				if(i==arrBtns.length-1) isBtnContainerUpdate=true;
				BtnContainer.append(temBtn,isBtnContainerUpdate);
			}
			BtnContainer.setTakeWH();
			contendContainer.append(BtnContainer,true,{t:6});
			contendContainer.setTakeWH();
			window.setContentWH(contendContainer.getShowWidth(),contendContainer.getShowHeight());
			window.getContainer().append(contendContainer);
		}
		private function resizeTxtContent():void{
			if(txtContent==null) return;
			txtContent.getTextField().width=window.getContainer().getShowWidth();
			txtContent.getTextField().height=window.getContainer().getShowHeight();
		}
		private function btnsClick(e:MouseEvent):void{
			var btnTem:CButton=e.target as CButton;
			var id:Number=btnTem.getOtherInfo().CAlertBtnsId;
			btnFun(id);
		}
		/**
		 * 显示或隐藏按钮组 
		 * @param isShow
		 * 
		 */		
		public function showHideBtns(isShow:Boolean=true):void{
			BtnContainer.visible=isShow;
		}
		/**
		 * 设置标题 文字
		 * @param str
		 * 
		 */		
		public function setTitle(str:String):void{
			this.window.setTitle(str);
		}
		/**
		 * 设置内容文字 
		 * @param str
		 * 
		 */		
		public function setContent(str:String):void{
			txtContent.text=str;
		}
		/**
		 * 获得窗口 
		 * @return 
		 * 
		 */		
		public function getFrame():CWindow{
			return this.window;
		}
		/**
		 * 获得弹出框用来放置内容文字，按钮组的容器 
		 * @return 
		 * 
		 */		
		public function getContainer():Container{
			return contendContainer
		}
		/**
		 * 获得内容文本框 
		 * @return 
		 * 
		 */		
		public function getTxtContent():CTextField{
			return txtContent;
		}
		/**
		 * 获得按钮组 
		 * @return 
		 * 
		 */		
		public function getBtns():Array{
			return arrBtns
		}
		override public function getShowWidth():Number
		{
			return window.getShowWidth();
		}
		override public function getShowHeight():Number
		{
			return window.getShowHeight();
		}
		override public function setWH(w:Number=0, h:Number=0):void{
			window.setWH(w,h);
			resizeTxtContent();
		}
	}
}